home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * Sketch.c
- *
- ****************************************************************************/
-
- #include "Assertion.h"
-
- #include "Structs.h"
- #include "Sketch.h"
-
- #include "AppleEvent.h"
- #include "DocumentADT.h"
- #include "OSASupport.h"
-
- #include "Windows.h"
- #include "Imaging.h"
-
- // ---------------------------------------------------------------
- // constants
-
- #define kDiskInitTop 0x0050 // disk init dialog coordinates
- #define kDiskInitLeft 0x0070
-
- #define kNILL_POINTER 0L
- #define kREMOVE_ALL_EVENTS 0
-
- // ---------------------------------------------------------------
- // Prototypes
-
- void main (void);
-
- static void Initialize (void);
- static void CleanUp (void);
-
- // Event processing
-
- static void EventLoop (void);
- static void ProcessEvent (EventRecord *event);
- static void ProcessIdleEvent (EventRecord *event);
-
- // event handler
-
- static void HandleMouseDown (EventRecord *event);
- static void HandleOSEvent (EventRecord *event);
- static void DoActivate (WindowPtr window, Boolean becomingActive);
- static void DoMenuCommand (long menuResult);
- static void DoUpdate (WindowPtr window);
-
- // menus
-
- static void HandleAppleChoice (short item);
- static void HandleFileChoice (short item);
- static void AdjustMenus (void);
-
- // windows
-
- static Boolean DoCloseWindow (WindowPtr window);
-
- // utilities
-
- static Boolean HasSystem7 (void);
-
- // content
-
- static void DrawWindowContents (WindowPtr window);
- static void DoContentClick (WindowPtr window, Point where);
-
- // ---------------------------------------------------------------
-
- static Boolean gQuitting;
- static Boolean gInBackground;
- static RgnHandle gCursorRgn;
-
- // ---------------------------------------------------------------
-
- void main(void)
- {
- Initialize();
- EventLoop();
- ZeroScrap();
- TEToScrap();
- }
-
- // ---------------------------------------------------------------
-
- #define NUM_MASTER_POINTER_BLOCKS 8
-
- static void
- Initialize(void)
- {
- short i;
- Handle menuBar;
-
- MaxApplZone();
- for (i=1; i<= NUM_MASTER_POINTER_BLOCKS; i++) {
- MoreMasters();
- }
-
- FlushEvents(everyEvent, kREMOVE_ALL_EVENTS);
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(kNILL_POINTER);
- InitCursor();
-
- gInBackground = false;
- gQuitting = false;
- gCursorRgn = NewRgn();
-
- TEFromScrap();
-
- menuBar = GetNewMBar(rMenuBar);
-
- if (menuBar)
- {
- SetMenuBar(menuBar);
- DisposHandle(menuBar);
- AddResMenu(GetMHandle(mApple), 'DRVR');
- DrawMenuBar();
- }
- else
- {
- gQuitting = true;
- return;
- }
-
- if (HasSystem7() == false)
- {
- Alert(rNotSystem7, kNILL_POINTER);
- gQuitting = true;
- return;
- }
-
- InitEditionPack();
- InstallAEHandlers();
-
- // Initialize the ADTs
-
- CreateDocumentList();
-
- // Intialize OSA, so we can load and run compiled scripts
-
- InitializeOSASupport();
-
- }
-
- // ---------------------------------------------------------------
-
- static void
- EventLoop(void)
- {
- Boolean gotEvent = false;
- EventRecord event;
- long sleepTime;
-
- while (gQuitting == false)
- {
- if (gInBackground)
- {
- sleepTime = -1L;
- }
- else
- {
- sleepTime = GetDblTime();
- }
-
- gotEvent = WaitNextEvent(everyEvent, &event, sleepTime, gCursorRgn);
-
- if (gotEvent)
- {
- ProcessEvent(&event);
- }
- else
- {
- ProcessIdleEvent(&event);
- }
- }
- }
-
- // ---------------------------------------------------------------
-
- static void
- ProcessEvent(EventRecord *event)
- {
- short error;
- char key;
- Point mountPoint;
-
- switch (event->what)
- {
- case activateEvt:
- DoActivate((WindowPtr)event->message, (event->modifiers & activeFlag) != 0);
- InitCursor();
- break;
-
- case diskEvt:
- InitCursor();
- if ((event->message >> 16) != noErr)
- {
- mountPoint.h = kDiskInitLeft;
- mountPoint.v = kDiskInitTop;
- error = DIBadMount(mountPoint, event->message);
- }
- break;
-
- case keyDown:
- case autoKey:
- key = event->message & charCodeMask;
- if (( event->modifiers & cmdKey ) != 0)
- {
- AdjustMenus();
- DoMenuCommand(MenuKey(key));
- }
- break;
-
- case mouseDown:
- HandleMouseDown(event);
- break;
-
- case osEvt:
- HandleOSEvent(event);
- break;
-
- case updateEvt:
- DoUpdate((WindowPtr)event->message);
- break;
-
- case kHighLevelEvent:
- AEProcessAppleEvent(event);
- break;
-
- default:
- ProcessIdleEvent(event);
- break;
-
- }
-
- }
-
- // ---------------------------------------------------------------
-
- static void
- ProcessIdleEvent(EventRecord *event)
- {
- #pragma unused (event)
-
- // do idle event activities here
- }
-
- // ---------------------------------------------------------------
-
- static void
- HandleMouseDown(EventRecord *event)
- {
- WindowPtr window;
- short windowPart;
-
- windowPart = FindWindow(event->where, &window);
-
- switch(windowPart)
- {
- case inMenuBar:
- AdjustMenus();
- DoMenuCommand(MenuSelect(event->where));
- break;
-
- #if 0
- case inSysWindow:
- SystemClick(&gTheEvent, whichWindow);
- break;
- #endif
-
- case inDrag:
- DragWindow(window, event->where, &qd.screenBits.bounds);
- break;
-
- case inGrow:
- break;
-
- case inContent:
- if (window != FrontWindow())
- {
- SelectWindow(window);
- }
- else {
- DoContentClick(window, event->where);
- }
- break;
-
- case inGoAway:
- if (TrackGoAway(window, event->where))
- {
- DoCloseWindow(window);
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(window, event->where, windowPart))
- {
- SetPort(window);
- EraseRect(&window->portRect);
- ZoomWindow(window, windowPart, true);
- InvalRect(&window->portRect);
- }
- break;
- }
- }
-
- // ---------------------------------------------------------------
-
- static void
- HandleOSEvent(EventRecord *event)
- {
- switch ((event->message >> 24) & 0xFF)
- {
- case suspendResumeMessage:
- if ((event->message & resumeFlag) == 0)
- {
- gInBackground = true; // suspend event
- ZeroScrap();
- TEToScrap();
- DoActivate(FrontWindow(), false); // deactive our front window
- }
- else
- {
- gInBackground = false; // resume event
- if (event->message & convertClipboardFlag)
- {
- TEFromScrap();
- }
- DoActivate(FrontWindow(), true); // active our front window
- }
- break;
-
- case mouseMovedMessage:
- DisposeRgn(gCursorRgn);
- gCursorRgn = NewRgn();
- SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
- break;
-
- case kHighLevelEvent:
- AEProcessAppleEvent(event);
- break;
-
- }
- }
-
- // ---------------------------------------------------------------
-
- static void
- DoMenuCommand(long menuChoice)
- {
- short menuID;
- short menuItem;
-
- menuID = HiWord(menuChoice);
- menuItem = LoWord(menuChoice);
-
- switch(menuID)
- {
- case mApple:
- HandleAppleChoice(menuItem);
- break;
-
- case mFile:
- HandleFileChoice(menuItem);
- break;
- }
-
- HiliteMenu(0);
- }
-
- // ---------------------------------------------------------------
-
- static void
- HandleAppleChoice(short item)
- {
- Str255 accName;
- short accNumber;
-
- switch (item)
- {
- case iAbout:
- Alert(rAboutBox, kNILL_POINTER);
- break;
-
- default:
- GetItem(GetMHandle(mApple), item, accName);
- accNumber = OpenDeskAcc(accName);
- break;
- }
- }
-
- // ---------------------------------------------------------------
-
- static void
- HandleFileChoice(short item)
- {
- switch (item)
- {
- case iQuit:
- DoQuit((DescType)0L);
- break;
-
- default:
- break;
- }
- }
-
- // ---------------------------------------------------------------
- // Also called from the RequiredSuite
-
- OSErr
- DoQuit(DescType saveOptions)
- {
- #pragma unused (saveOptions)
-
- OSErr error = noErr;
-
- CleanUp();
- gQuitting = true;
-
- return error;
- }
-
- #pragma mark -
-
- // ---------------------------------------------------------------
-
- static Boolean
- DoCloseWindow(WindowPtr window)
- {
- DocumentReference document;
-
- if (window != NULL)
- {
- SetPort(window);
- document = (DocumentReference)GetWRefCon(window);
-
- if (document != nil)
- DestroyDocument(document);
- }
- return true;
- }
-
- // ---------------------------------------------------------------
-
- static void
- CleanUp(void)
- {
- WindowPtr window;
- Boolean closed = true;
-
- do
- {
- window = FrontWindow();
- if (window)
- closed = DoCloseWindow(window);
- } while (closed && window);
-
- gQuitting = closed;
- }
-
- // ---------------------------------------------------------------
-
- static void
- DoActivate(WindowPtr window, Boolean becomingActive)
- {
- #pragma unused (window)
-
- if (becomingActive)
- {
- // do activate stuff
- // •••
- }
- else
- {
- // do deactivate stuff
- // •••
- }
- }
-
- // ---------------------------------------------------------------
-
- static void
- DoUpdate(WindowPtr window)
- {
- SetPort(window);
- BeginUpdate(window);
-
- if (!EmptyRgn(window->visRgn))
- {
- DrawWindowContents(window);
- }
-
- EndUpdate(window);
- }
-
- // ---------------------------------------------------------------
-
- static void
- DrawWindowContents(WindowPtr window)
- {
- DocumentReference document;
-
- document = (DocumentReference)GetWRefCon(window);
-
- IMDraw(document);
- }
-
- // ---------------------------------------------------------------
-
- static void
- AdjustMenus (void)
- {
- WindowPtr window;
- MenuHandle fileMenu;
-
- window = FrontWindow();
- fileMenu = GetMHandle(mFile);
-
- EnableItem(fileMenu, iQuit);
- }
-
- // ---------------------------------------------------------------
-
- static void
- DoContentClick(WindowPtr window, Point where)
- {
- #pragma unused (window, where)
-
- InitCursor();
- SysBeep(2);
- }
-
- // ---------------------------------------------------------------
-
- static Boolean HasSystem7(void)
- {
- long response;
- OSErr error;
-
- error = Gestalt(gestaltSystemVersion, &response);
- if (error == noErr)
- if ((response & 0x0000FFFF) < 0x0700)
- error = -1;
-
- return (error == noErr);
- }
-
- // ---------------------------------------------------------------
- // Determine if the current process is in the background.
- // ---------------------------------------------------------------
-
- Boolean IsCurrentProcessInBackground(void)
- {
- Boolean inBackground = true;
-
- ProcessSerialNumber currentPSN;
- ProcessSerialNumber frontPSN;
- Boolean isSameProcess;
-
- if (GetFrontProcess(&frontPSN) == noErr)
- if (GetCurrentProcess(¤tPSN) == noErr)
- if (SameProcess(&frontPSN, ¤tPSN, &isSameProcess) == noErr)
- if (isSameProcess)
- inBackground = false;
-
- return inBackground;
- }
-
- // ---------------------------------------------------------------
-
-
-
-